import sqlite3
import os

def count_db_records(db_path):
    try:
        # 检查文件是否存在
        if not os.path.exists(db_path):
            print(f"文件 {db_path} 不存在")
            return None
        
        # 连接数据库
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 获取表列表
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        
        results = {}
        # 查询每个表的记录数
        for (table_name,) in tables:
            cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
            count = cursor.fetchone()[0]
            results[table_name] = count
        
        conn.close()
        return results
    except Exception as e:
        print(f"查询 {db_path} 时出错: {e}")
        return None

# 主程序
print("查询数据库记录数量...")
print("=" * 40)

# 查询 jntujy.db
print("\n查询 jntujy.db:")
print("-" * 40)
db1_results = count_db_records("jntujy.db")
if db1_results:
    total1 = 0
    for table, count in db1_results.items():
        print(f"{table}: {count} 条记录")
        total1 += count
    print(f"总记录数: {total1}")

# 查询 jntujy11.db
print("\n查询 jntujy11.db:")
print("-" * 40)
db2_results = count_db_records("jntujy11.db")
if db2_results:
    total2 = 0
    for table, count in db2_results.items():
        print(f"{table}: {count} 条记录")
        total2 += count
    print(f"总记录数: {total2}")

# 比较差异
if db1_results and db2_results:
    print("\n数据量比较:")
    print("-" * 40)
    print(f"jntujy.db 总记录数: {sum(db1_results.values())}")
    print(f"jntujy11.db 总记录数: {sum(db2_results.values())}")
    print(f"差异: {abs(sum(db1_results.values()) - sum(db2_results.values()))} 条记录")